package com.kc.gb.device.common.dto;

import com.kc.gb.device.common.enums.ErrorCode;
import com.kc.gb.device.common.utils.I18NHelper;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.i18n.LocaleContextHolder;

import java.util.Locale;

@Slf4j
@Data
@EqualsAndHashCode(callSuper = true)
public class I18NException extends RuntimeException {
    private String code;
    private String message;

    public I18NException(ErrorCode code, Object... messageParams) {
        this.code = code.getCode();
        String localeCode = "device.error." + code.name();
        Locale locale = LocaleContextHolder.getLocale();
        log.info("I18NException localeCode:{}, locale:{}", localeCode, locale);
        this.message = I18NHelper.getLocaleMessage(LocaleContextHolder.getLocale(),localeCode,messageParams);
    }
}
